package com.wzg.logrlibrary;

import android.annotation.SuppressLint;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;

public class LogR {

    private static volatile LogR instance;

    private static boolean logE = false;
    private static boolean logD = false;
    private static boolean logI = false;
    private static boolean logV = false;
    private static boolean logW = false;

    private static int dateFormatType = -1;//默认带时分秒

    public static final int YYYY_MM_DD_HH_MM_SS = -1;//日期的格式化类型  带时分秒
    public static final int YYYY_MM_DD = 1;//日期的格式化类型  无时分秒

    private static String TAG = "联网";

    private static String TAG_E = "null";
    private static String TAG_D = "null";
    private static String TAG_I = "null";
    private static String TAG_V = "null";
    private static String TAG_W = "null";


    //不直接给外部使用实例，因为没有必要
    private LogR() {
    }

    @NonNull
    public static LogR getInstance() {
        if (instance == null) instance = new LogR();
        return instance;
    }


    public LogR setTAG(@NonNull String TAG) {
        LogR.TAG = TAG;
        return getInstance();
    }


    public LogR setLogE(boolean logE) {
        LogR.logE = logE;
        return getInstance();
    }

    public LogR setLogD(boolean logD) {
        LogR.logD = logD;
        return getInstance();
    }

    public LogR setLogI(boolean logI) {
        LogR.logI = logI;
        return getInstance();
    }

    public LogR setLogV(boolean logV) {
        LogR.logV = logV;
        return getInstance();
    }

    public LogR setLogW(boolean logW) {
        LogR.logW = logW;
        return getInstance();
    }

    // TODO: 2021/3/22  wzg  addTagE
    //给指定的级别指定一个tag
    public LogR setLogE(@NonNull String tagE, boolean logE) {
        TAG_E = tagE;
        LogR.logE = logE;
        return getInstance();
    }

    public LogR setLogD(@NonNull String tagD, boolean logD) {
        TAG_D = tagD;
        LogR.logD = logD;
        return getInstance();
    }

    public LogR setLogI(@NonNull String tagI, boolean logI) {
        TAG_I = tagI;
        LogR.logI = logI;
        return getInstance();
    }

    public LogR setLogV(@NonNull String tagV, boolean logV) {
        TAG_E = tagV;
        LogR.logV = logV;
        return getInstance();
    }

    public LogR setLogW(@NonNull String tagV, boolean logW) {
        TAG_V = tagV;
        LogR.logW = logW;
        return getInstance();
    }


    public LogR setDateFormatType(int dateType) {
        LogR.dateFormatType = dateType;
        return getInstance();
    }

    public int getDateFormatType() {
        return dateFormatType;
    }

    public static boolean isLogE() {
        return logE;
    }

    public static boolean isLogD() {
        return logD;
    }

    public static boolean isLogI() {
        return logI;
    }

    public static boolean isLogV() {
        return logV;
    }

    public static boolean isLogW() {
        return logW;
    }

    public String getTAG() {
        return TAG;
    }

    //初始化配置
    public void init(@NonNull String TAG, boolean logE, boolean logD, boolean logI, boolean logV, boolean logW) {
        LogR.TAG = TAG;
        LogR.logE = logE;
        LogR.logD = logD;
        LogR.logI = logI;
        LogR.logV = logV;
        LogR.logW = logW;
    }

    //默认配置
    public void initDefault() {
        LogR.TAG = "联网";
        LogR.logE = true;
        LogR.logD = false;
        LogR.logI = false;
        LogR.logV = true;
        LogR.logW = false;
    }


    public static void logE(@NonNull String msg) {

        if (isLogE()) {
            changeTag();
            Log.e(getInstance().getTAG(), "logE: " + msg);
        }
    }

    //带日期格式化
    public static void logDateE(@NonNull String msg) {
        if (isLogE()) {
            changeTag();
            Log.e(getInstance().getTAG(), TimeUtil.date() + "\tlogE: " + msg);
        }
    }

    public static void logE(@NonNull String TAG, @NonNull String msg) {
        if (isLogE()) {
            changeTag();
            Log.e(TAG, "logE: " + msg);
        }
    }

    //带日期格式化
    public static void logDateE(@NonNull String TAG, @NonNull String msg) {
        if (isLogE()) {
            changeTag();
            Log.e(TAG, TimeUtil.date() + "\tlogE: " + msg);
        }
    }

    public static void logD(@NonNull String msg) {
        if (isLogD()) {
            changeTag();
            Log.d(getInstance().getTAG(), "logD: " + msg);
        }
    }

    //带日期格式化
    public static void logDateD(@NonNull String msg) {
        if (isLogD()) {
            changeTag();
            Log.d(getInstance().getTAG(), TimeUtil.date() + "\tlogD: " + msg);
        }
    }

    public static void logD(@NonNull String TAG, @NonNull String msg) {
        if (LogR.isLogD()) {
            changeTag();
            Log.d(TAG, "logD: " + msg);
        }
    }

    //带日期格式化
    public static void logDateD(@NonNull String TAG, @NonNull String msg) {
        if (isLogD()) {
            changeTag();
            Log.d(TAG, TimeUtil.date() + "\tlogD: " + msg);
        }
    }

    public static void logV(@NonNull String msg) {
        if (isLogV()) {
            changeTag();
            Log.v(getInstance().getTAG(), "logV: " + msg);
        }
    }

    //带日期格式化
    public static void logDateV(@NonNull String msg) {
        if (isLogV()) {
            changeTag();
            Log.v(getInstance().getTAG(), TimeUtil.date() + "\tlogV: " + msg);
        }
    }

    public static void logV(@NonNull String TAG, String msg) {
        if (isLogV()) {
            changeTag();
            Log.v(TAG, "logV: " + msg);
        }
    }

    //带日期格式化
    public static void logDateV(@NonNull String TAG, @NonNull String msg) {
        if (isLogV()) {
            changeTag();
            Log.v(TAG, TimeUtil.date() + "\tlogV: " + msg);
        }
    }

    public static void logI(@NonNull String msg) {
        if (LogR.isLogI()) {
            changeTag();
            Log.i(getInstance().getTAG(), "logI: " + msg);
        }
    }

    //带日期格式化
    public static void logDateI(@NonNull String msg) {
        if (isLogI()) {
            changeTag();
            Log.i(getInstance().getTAG(), TimeUtil.date() + "\tlogI: " + msg);
        }
    }

    public static void logI(@NonNull String TAG, @NonNull String msg) {
        if (isLogI()) {
            changeTag();
            Log.i(TAG, "logI: " + msg);
        }
    }

    //带日期格式化
    public static void logDateI(@NonNull String TAG, @NonNull String msg) {
        if (isLogI()) {
            changeTag();
            Log.i(TAG, TimeUtil.date() + "\tlogI: " + msg);
        }
    }

    public static void logW(@NonNull String msg) {
        if (isLogW()) {
            changeTag();
            Log.w(getInstance().getTAG(), "logW: " + msg);
        }
    }

    //带日期格式化
    public static void logDateW(@NonNull String msg) {
        if (isLogW()) {
            changeTag();
            Log.w(getInstance().getTAG(), TimeUtil.date() + "\tlogW: " + msg);
        }
    }

    public static void logW(@NonNull String TAG, @NonNull String msg) {
        if (isLogW()) {
            changeTag();
            Log.w(TAG, "logW: " + msg);
        }
    }


    //带日期格式化LogW
    public static void logDateW(@NonNull String TAG, @NonNull String msg) {
        if (isLogW()) {
            changeTag();
            Log.w(TAG, TimeUtil.date() + "\tlogW: " + msg);
        }
    }


    /**
     * @param tagStr 你指定tag级别，只能是字符串比如：logE
     * @param json   你要打印的json字符串
     */
    public static void logJsonS(@NonNull String tagStr, @Nullable String json) {
        if (Utils.isEmpty(json)) {
            logDataS(tagStr, "Empty/Null json content");
            return;
        }
        try {
            json = json.trim();
            if (json.startsWith("{")) {
                JSONObject jsonObject = new JSONObject(json);
                String message = jsonObject.toString(2);
                logDataS(tagStr, message);
                return;
            }
            if (json.startsWith("[")) {
                JSONArray jsonArray = new JSONArray(json);
                String message = jsonArray.toString(2);
                logDataS(tagStr, message);
                return;
            }
            logDataS(tagStr, "Invalid Json");
        } catch (JSONException e) {
            logDataS(tagStr, "Invalid Json");
        }
    }


  /*  public static void logJsonS(@NonNull String tagStr, @Nullable JSONObject json) {
        if (json == null || json.toString().length() == 0) {
            logDataS(tagStr, "Empty/Null json content");
            return;
        }
        try {
            // java 对象转 json字符串

            logDataS(tagStr, "Invalid Json");
        } catch (JSONException e) {
            logDataS(tagStr, "Invalid Json");
        }
    }*/


    public static void logJsonE(@Nullable String json) {
        if (Utils.isEmpty(json)) {
            logDateE("Empty/Null json content");
            return;
        }
        try {
            json = json.trim();
            if (json.startsWith("{")) {
                JSONObject jsonObject = new JSONObject(json);
                String message = jsonObject.toString(2);
                logDateE(message);
                return;
            }
            if (json.startsWith("[")) {
                JSONArray jsonArray = new JSONArray(json);
                String message = jsonArray.toString(2);
                logDateE(message);
                return;
            }
            logDateE("Invalid Json");
        } catch (JSONException e) {
            logDateE("Invalid Json");
        }
    }


    public static class TimeUtil {
        /**
         * 默认日期格式
         */
        private static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";

        /**
         * 字符型日期格式
         */
        public static final String TIME_FORMAT = "yyyy-MM-dd";

        /**
         * 获取当前时间并将其格式化
         * 默认是带时分秒的
         */
        @SuppressLint("SimpleDateFormat")
        public static String date() {
            String typeStr;
            if (getInstance().getDateFormatType() == YYYY_MM_DD_HH_MM_SS) {//默认值
                typeStr = DEFAULT_FORMAT;
            } else {
                typeStr = TIME_FORMAT;
            }
            return new SimpleDateFormat(typeStr).format(System.currentTimeMillis());
        }
    }


    //
    private static void changeTag() {
        if (isNonNullStr(TAG_E)) getInstance().setTAG(TAG_E);
        if (isNonNullStr(TAG_V)) getInstance().setTAG(TAG_V);
        if (isNonNullStr(TAG_I)) getInstance().setTAG(TAG_I);
        if (isNonNullStr(TAG_W)) getInstance().setTAG(TAG_W);
        if (isNonNullStr(TAG_D)) getInstance().setTAG(TAG_D);
    }


    private static boolean isNonNullStr(String tag) {
        return tag != null && !tag.equals("") && !tag.equals("null") && !tag.equals("NULL");
    }


    /**
     * @param tag 输入名称只能是字符串对应的字符
     * @param arg 要打印的内容
     */
    private static void logDataS(@NonNull String tag, String arg) {
        switch (tag) {
            case "logE":
                logDateE(arg);
                break;
            case "logV":
                logDateV(arg);
                break;
            case "logW":
                logDateW(arg);
                break;
            case "logD":
                logDateD(arg);
                break;
            case "logI":
                logDateI(arg);
                break;
            default:
                logDateE(arg);
                break;
        }

    }


}
